package com.thesis.manage.service.impl;

import com.github.crab2died.ExcelUtils;
import com.thesis.manage.common.constant.YesOrNoEnum;
import com.thesis.manage.common.exception.CommonException;
import com.thesis.manage.common.service.impl.BaseServiceImpl;
import com.thesis.manage.domain.College;
import com.thesis.manage.mapper.CollegeMapper;
import com.thesis.manage.service.CollegeService;
import com.thesis.manage.service.constant.ErrorMessage;
import com.thesis.manage.service.module.excel.CollegeExcel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.common.Mapper;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author php
 * @date 2018/1/12
 */
@Service
@Slf4j
public class CollegeServiceImpl extends BaseServiceImpl<College> implements CollegeService {

    @Resource
    private CollegeMapper collegeMapper;

    @Override
    protected Mapper<College> getMapper() {
        return this.collegeMapper;
    }

    @Override
    protected Class<College> getEntityClass() {
        return College.class;
    }


    @Override
    public void importExcel(MultipartFile file) {
        Assert.notNull(file, ErrorMessage.REQUEST_SELECT_FILE);
        try {
            //导入学院信息
            List<CollegeExcel> collegeExcels = ExcelUtils.getInstance().readExcel2Objects(file.getInputStream(), CollegeExcel.class);
            /*
            对象转换
             */
            List<College> convert = this.convert(collegeExcels);
            for (College college : convert) {
                if (this.isNotExist(college.getName())) {
                    this.save(college);
                }
            }
        } catch (Exception e) {
            throw new CommonException("file import failed", ErrorMessage.FILE_IMPORT_FAILED);
        }
    }

    @Override
    public void export(HttpServletResponse response) {
        try {
            String name = "学校院系信息.xlsx";
            // 设置response参数，可以打开下载页面
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes(), "iso-8859-1"));
            ServletOutputStream out = response.getOutputStream();
            College college = new College();
            college.setDelStatus(YesOrNoEnum.NO.getValue());
            List<College> list = this.collegeMapper.select(college);
            if (CollectionUtils.isNotEmpty(list)) {
                //导出学院信息
                ExcelUtils.getInstance().exportObjects2Excel(list, CollegeExcel.class, true, "sheet0", true, out);
            }
        } catch (Exception e) {
            throw new CommonException("file export failed", ErrorMessage.FILE_EXPORT_ERROR);
        }

    }

    @Override
    public College findByName(String name) {
        College college = new College();
        college.setName(name);
        return this.findByOne(college);
    }

    @Override
    public List<College> findAll() {
        College college = new College();
        college.setDelStatus(YesOrNoEnum.NO.getValue());
        return this.getMapper().select(college);
    }


    /**
     * 验证院系是否存在
     *
     * @param name 院系名称
     * @return boolean
     */
    private boolean isNotExist(String name) {
        College college = new College();
        college.setName(name);
        college.setDelStatus(YesOrNoEnum.NO.getValue());
        return this.collegeMapper.selectOne(college) == null;
    }

    /**
     * 对象转换
     *
     * @param collegeExcels 转换对象
     * @return list<College>
     */
    private List<College> convert(List<CollegeExcel> collegeExcels) {
        List<College> list = new ArrayList<>();
        try {
            if (CollectionUtils.isNotEmpty(collegeExcels)) {
                College college;
                for (CollegeExcel collegeExcel : collegeExcels) {
                    college = new College();
                    PropertyUtils.copyProperties(college, collegeExcel);
                    list.add(college);
                }
            }
            return list;
        } catch (Exception e) {
            throw new CommonException("object covert failed", ErrorMessage.OBJECT_CONVERT_FAILED);
        }
    }
}
